﻿using System.IO;
using NAnt.Core;
using NAnt.Core.Attributes;
using NAnt.Core.Types;

namespace NAnt.SVN.Tasks
{
    /// <summary>
    /// Implementation of the lock command. Only the file
    /// mode is supported.
    /// You can run this tasc with the svnlock tag in NAnt.
    /// $Id$
    /// </summary>
    [TaskName("svnlock")]
    public class Lock : SVNTask
    {
        #region Private Fields

        string targets = string.Empty;
        string user = string.Empty;
        string password = string.Empty;
        string revision = string.Empty;
        bool noauthcache = false;
        bool force = true;
        bool noninteractive = true;

        #endregion

        #region Public Fields

        /// <summary>
        /// The file to set the properties to
        /// </summary>
        [TaskAttribute("file", Required = true)]
        public virtual FileInfo File
        {
            get;
            set;
        }

        /// <summary>
        /// Username
        /// </summary>
        [TaskAttribute("user", Required = false)]
        [StringValidator()]
        public string User
        {
            get
            {
                return user;
            }
            set
            {
                user = value;
            }
        }

        /// <summary>
        /// Password
        /// </summary>
        [TaskAttribute("password", Required = false)]
        [StringValidator()]
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

        /// <summary>
        /// Don't store the user credentials if true
        /// </summary>
        [TaskAttribute("noauthcache", Required = false)]
        [BooleanValidator()]
        public bool NoAuthCache
        {
            get
            {
                return noauthcache;
            }
            set
            {
                noauthcache = value;
            }
        }

        /// <summary>
        /// Force the lock
        /// </summary>
        [TaskAttribute("force", Required = false)]
        [BooleanValidator()]
        public bool Force
        {
            get
            {
                return force;
            }
            set
            {
                force = value;
            }
        }

        /// <summary>
        /// Check if the checkout has to be performed with 
        /// authentication or not.
        /// </summary>
        public bool HasCredentials
        {
            get
            {
                return !string.IsNullOrEmpty(User) &&
                    !string.IsNullOrEmpty(Password);
            }
        }

        /// <summary>
        /// Run the command non interactive
        /// Currently always set to true.
        /// </summary>
        public bool NonInteractive
        {
            get
            {
                return noninteractive;
            }
        }

        #endregion

        /// <summary>
        /// Initialize command
        /// </summary>
        protected override void InitializeSVNTask()
        {
            Command = SVNCommands.LOCK;
            
            //Add the file
            Options.Add(SVNFlags.FILE);
            Options.Add(quote(File.FullName));

            //Check the conditions
            /*if (Files == null && File == null)
            {
                throw new BuildException(string.Format(getString("SVN0305"), "File, Fileset"), Location);
            }
            //Fileset overrules the file
            else if (Files == null && File != null)
            {
                Files = new FileSet();
                Files.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
                Files.FileNames.Add(File.FullName);
            }*/

            //Add the credentials
            if (HasCredentials)
            {
                Options.Add(SVNFlags.USER);
                Options.Add(quote(User));
                Options.Add(SVNFlags.PASSWORD);
                Options.Add(quote(Password));
                if (NoAuthCache) Options.Add(SVNFlags.NOAUTHCACHE);
            }
            //Noninteractive and force attributes
            if (NonInteractive) Options.Add(SVNFlags.NONINTERACTIVE);
            if (Force) Options.Add(SVNFlags.FORCE);
            //
            base.InitializeSVNTask();
        }
    }
}